#include <iostream>
#include <string>
using namespace std;
string sr[105],sc[105];
int main() {
	int sum = 0;
	string s;
	getline(cin,s);
	int n = stoi(s);
	for(int i = 0; i < n; i++) {
		string sh;
		getline(cin,sh);
		int temp = (int)sh.at(0)-65;
		for(int i = 2; i < sh.length(); i++) {
			sr[temp] += sh.at(i);
		}
	}
	for(string s; getline(cin, s);){
		sc[sum] = s;
		//cout<<sc[sum].length()<<endl;
		for(int i = 0; i < sc[sum].length(); i++){
			//cout<<1<<endl;
		    //cout<<sc[sum].length()<<endl; 
			if(sc[sum].at(i) == '$'){
				i++;
				if(sc[sum].at(i) == '$') {
					sc[sum] = sc[sum].replace(i,1,"");
					//cout<<sc[sum].at(i)<<endl;
					//cout<<sc[sum].at(i+1)<<endl;
					i--;
				}
				else{
					for(int j = 0; j <= 25; j++){
						//cout<<j<<endl;
						if((int)sc[sum].at(i)-65 == j){
							//cout<<sc[sum].length()<<endl;
							sc[sum] = sc[sum].replace(i-1,2,sr[j]);
							//cout<<sc[sum]<<endl;
							//cout<<sc[sum].length();
							break;
					
						}
						
					}
					
				}
			}
		}
		sum++;
	}
	for(int i = 0; i < sum; i++){
		cout<<sc[i]<<endl;
	}
	return 0;
}
